﻿using Sprint1.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sprint1.Controllers
{
    public class PollsController : Controller
    {
        //
        // GET: /Polls/
        public ActionResult Index(int courseId)
        {
            StudyDatabaseEntities db = new StudyDatabaseEntities();
            List<QuestionPoll> questions = db.QuestionPolls.Where(que => que.CourseId == courseId).ToList();
            ViewData["CourseName"] = db.Courses.Single(cou => cou.Id == courseId).Name;
            ViewData["CourseId"] = courseId;
            
            return View(questions);
        }
        
        [HttpGet]
        public ActionResult Create(int CourseId)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(int CourseId, FormCollection formCollection)
        {
            StudyDatabaseEntities db = new StudyDatabaseEntities();
            QuestionPoll questionPoll = new QuestionPoll();
            questionPoll.Text = formCollection["Text"];
            questionPoll.CourseId = CourseId;
            questionPoll.InstructorId = 1 ;// db.Instructors.Single(ins => ins.Email == HttpContext.User.Identity.Name).Id;
            db.QuestionPolls.Add(questionPoll);
            db.SaveChanges();

            questionPoll = db.QuestionPolls.OrderByDescending(que => que.Id).First();
            int questionId = questionPoll.Id;

            formCollection["Ar"] += ',';
            for (int countOptions = 0, start = 0; countOptions < Convert.ToInt32(formCollection["NOA"]); countOptions++)
            {
                AnswerPoll answerPoll = new AnswerPoll();
                string str = formCollection["Ar"];
                answerPoll.Text = str.Substring(start, str.IndexOf(',', start) - start);
                start = str.IndexOf(',', start) + 1;
                answerPoll.QuestionId = questionId;
                answerPoll.Vote = 0.0;
                db.AnswerPolls.Add(answerPoll);
            }
            db.SaveChanges();
            return RedirectToAction("Index", new { courseId = CourseId});
        }
       /* [HttpPost]
        public ActionResult Create(FormCollection formCollection)
        {
            QuestionPoll questionPoll = new QuestionPoll();
            int courseId = -1;
            return RedirectToAction("Index", new { courseId = courseId });
        }*/
        [HttpGet]
        public ActionResult Details(int questionId)
        {
            StudyDatabaseEntities db = new StudyDatabaseEntities();
            List<AnswerPoll> answers = db.AnswerPolls.Where(ans => ans.QuestionId == questionId).ToList();
            @ViewBag.QuestionText = db.QuestionPolls.Single(que => que.Id == questionId).Text;
            return View(answers);
        }
        public ActionResult DetailsPostback(int questionId, FormCollection formCollection)
        {
            string email= HttpContext.User.Identity.Name;
            StudyDatabaseEntities db = new StudyDatabaseEntities();
            int studentId = 1;// db.Students.Single(stu => stu.Email == email).Id;
            StudentAnswerPoll studentAnswerPoll = new StudentAnswerPoll();
            studentAnswerPoll.QuestionId = questionId;
            studentAnswerPoll.AnswerId = 1009; // EDIT
            studentAnswerPoll.StudentId = studentId; // EDIT
            db.StudentAnswerPolls.Add(studentAnswerPoll);
            db.SaveChanges();
            // Edit Vote Percentage
            List<AnswerPoll> answers = db.AnswerPolls.Where(ans => ans.QuestionId == questionId).ToList();
            foreach (var answer in answers)
            {
                int answerCount = db.StudentAnswerPolls.Count(ans => ans.AnswerId == answer.Id);
                int questionCount = db.StudentAnswerPolls.Count(que => que.QuestionId == questionId);
                float count = (float)answerCount * 100 / questionCount;
                answer.Vote = count;

                db.AnswerPolls.Attach(answer);
               db.Entry(answer).State = EntityState.Modified;
                db.SaveChanges();
            }
            int courseId = db.QuestionPolls.Single(que => que.Id == questionId).CourseId;
            return RedirectToAction("Index", new { courseId = courseId });
        }
        
        public ActionResult Results(int questionId)
        {
            StudyDatabaseEntities db = new StudyDatabaseEntities();
            List<AnswerPoll> answers = db.AnswerPolls.Where(ans => ans.QuestionId == questionId).OrderByDescending(ans => ans.Vote).ToList();
            @ViewBag.QuestionText = db.QuestionPolls.Single(que => que.Id == questionId).Text;
            return View(answers);
        }

    }
}
